<?php

namespace app\admin\model;

use app\common\enums\StatusEnum;
use think\Db;

class LangClassRecite extends \app\common\model\LangClassRecite
{
    /**
     * 背单词或者背句子列表
     * @param $classId
     * @param $courseType
     * @param $type
     * @param $typeId
     * @param array $where
     * @return array
     * @throws \think\exception\DbException
     */
    public static function getList($classId, $courseType, $type, $typeId, $where = [])
    {
        $sql = static::field('id, class_id, course_type, type, type_id, word, sort_num, status, create_time, update_time')
            ->where(['class_id' => $classId, 'course_type' => $courseType, 'type' => $type, 'type_id' => $typeId]);

        if ($where) {
            $sql->where($where);
        }

        $data = $sql->paginate(null, false, ['query' => request()->param()]);

        $typeIds = [];
        foreach ($data->items() as $key => $value) {
            $typeIds[$value['type_id']] = $value['type_id'];
        }

        $type = [];
        if ($typeIds) {
            $type = LangClassReciteType::getColumn($typeIds);
        }

        $model = $data->each(function ($item, $key) use ($type) {
            $item['typeName'] = isset($type[$item['type_id']]) ? $type[$item['type_id']] : '未设置';
            $item['create_time'] = get_datetime($item['create_time']);
            $item['update_time'] = get_datetime($item['update_time']);
            $item['statusText'] = StatusEnum::getStatus($item['status']);
            return $item;
        });

        // 获取总记录数
        $count = $model->total();
        return [
            'model' => $model,
            'count' => $count
        ];
    }

    /**
     * 添加修改背单词或者背句子
     * @param $data
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * @throws \Exception
     */
    public static function saveOne($data)
    {
        $saveData = [
            'class_id' => $data['class_id'],
            'course_type' => $data['course_type'],
            'type' => $data['type'],
            'type_id' => $data['type_id'],
            'file' => $data['file'],
            'size' => $data['fileSize'],
            'word' => $data['word'],
            'sort_num' => $data['sort_num'],
            'status' => $data['status'],
            'update_time' => time()
        ];

        if (isset($data['id']) && $data['id'] > 0) {
            Db::name('lang_class_recite')
                ->where('id', $data['id'])
                ->data($saveData)
                ->update();
            $id = $data['id'];
        } else {
            $saveData['create_time'] = time();
            $id = Db::name('lang_class_recite')->insertGetId($saveData);
        }

        // 添加答案
        if ($id > 0) {
            LangClassReciteAnswer::saveData($id, $data['is_right'], $data['answer']);
        }

        return $id;
    }

    /**
     * 批量禁用启用
     * @param $ids
     * @param $status
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function disables($ids, $status)
    {
        static::where('id', 'in', $ids)->update(['status' => $status]);
    }

    public static function deleteAll($ids)
    {
        static::where('id', 'in', $ids)->delete();
    }
}